package com.zzt.zt_scaleparentviewgroup;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;


/**
 * @author: zeting
 * @date: 2022/8/14
 * 将字视图缩放的布局
 */
public class ScaleParentSquareViewGroup extends FrameLayout {
    private static final String TAG = ScaleParentSquareViewGroup.class.getSimpleName();
    private int childMaxWidth = 0;
    private int childMaxHeight = 0;
    private int parentWidth = 0;
    private int parentHeight = 0;
    private View childView;// 子布局
    //   宽，高，是否固定
    private boolean isFixedWidth = false;
    private boolean isFixedHeight = false;
    // 是不是正方向
    private boolean isSquare = false;

    public ScaleParentSquareViewGroup(@NonNull Context context) {
        super(context);
        init(context, null, 0);
    }

    public ScaleParentSquareViewGroup(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs, 0);
    }

    public ScaleParentSquareViewGroup(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs, defStyleAttr);
    }

    private void init(Context context, AttributeSet attrs, int defStyleAttr) {
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.ScaleParentView, defStyleAttr, 0);
        isFixedWidth = typedArray.getBoolean(R.styleable.ScaleParentView_is_fixed_width, true);
        isFixedHeight = typedArray.getBoolean(R.styleable.ScaleParentView_is_fixed_height, false);
        isSquare = typedArray.getBoolean(R.styleable.ScaleParentView_is_square, false);
        typedArray.recycle();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        parentWidth = getMeasuredWidth();
        parentHeight = getMeasuredHeight();
        Log.d(TAG, "父布局   最大宽：" + parentWidth + " 最大高：" + parentHeight);
        if (getChildCount() > 0) {
            childView = getChildAt(0);
            int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(getScreenWidth(), MeasureSpec.EXACTLY);
            int childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(childView.getMeasuredHeight(), MeasureSpec.EXACTLY);
            childView.measure(childWidthMeasureSpec, childHeightMeasureSpec);

            childMaxWidth = childView.getMeasuredWidth();
            childMaxHeight = childView.getMeasuredHeight();
            Log.d(TAG, "子布局  最大宽：" + childMaxWidth + " 最大高：" + childMaxHeight);
        }
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);

        double hScale = NumberUtil.divide(parentHeight, childMaxHeight);
        double wScale = NumberUtil.divide(parentWidth, childMaxWidth);
        Log.d(TAG, "宽高缩放比例   宽：" + wScale + "  高：" + hScale);

        if (childView != null) {
            float min = (float) Math.min(hScale, wScale);
            childView.setScaleY(min);
            childView.setScaleX(min);
        }
    }

    public int getScreenWidth() {
        DisplayMetrics outMetrics = new DisplayMetrics();
        WindowManager wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
        wm.getDefaultDisplay().getMetrics(outMetrics);
        int widthPixels = outMetrics.widthPixels;
        return widthPixels;
    }

    public int getScreenHeight() {
        DisplayMetrics outMetrics = new DisplayMetrics();
        WindowManager wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
        wm.getDefaultDisplay().getMetrics(outMetrics);
        int heightPixels = outMetrics.heightPixels;
        return heightPixels;
    }

}
